package splug.cjk;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class BBSCarPoolTab1Write extends Activity {

	private Button write;
	private Button back;
	private Spinner start1;
	private Spinner start2;
	private Spinner finish1;
	private Spinner finish2;
	private Spinner time1;
	private Spinner time2;
	private EditText text_title;
	private EditText text;
	private ArrayAdapter<String> adapter;
	private SearchOption option = new SearchOption();
	private String id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("카풀 게시판 글쓰기");
		setContentView(R.layout.bbs_carpool_tab1_write);

		Bundle b = this.getIntent().getExtras();
		id = String.valueOf(b.getString("ID"));

		write = (Button) findViewById(R.id.btn_carpool_tab1_write_ok);
		back = (Button) findViewById(R.id.btn_carpool_tab1_write_cancel);
		start1 = (Spinner) findViewById(R.id.spin_tab1_write_1);
		start2 = (Spinner) findViewById(R.id.spin_tab1_write_2);
		finish1 = (Spinner) findViewById(R.id.spin_tab1_write_3);
		finish2 = (Spinner) findViewById(R.id.spin_tab1_write_4);
		time1 = (Spinner) findViewById(R.id.spin_tab1_write_5);
		time2 = (Spinner) findViewById(R.id.spin_tab1_write_6);
		text = (EditText) findViewById(R.id.ed_tab1_write_content);
		text_title = (EditText) findViewById(R.id.ed_tab1_write_title);

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, option.city);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);  
		start1.setAdapter(adapter);
		finish1.setAdapter(adapter);

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, option.nothing);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);  
		start2.setAdapter(adapter);
		finish2.setAdapter(adapter);

		start1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(arg2 == 1){
					ArrayList<String> seoul = option.seoul;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 2){
					ArrayList<String> seoul = option.busan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 3){
					ArrayList<String> seoul = option.daegu;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 4){
					ArrayList<String> seoul = option.incheon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 5){
					ArrayList<String> seoul = option.gwangju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 6){
					ArrayList<String> seoul = option.daejun;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 7){
					ArrayList<String> seoul = option.ulsan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 8){
					ArrayList<String> seoul = option.kyonggi;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 9){
					ArrayList<String> seoul = option.kangwon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 10){
					ArrayList<String> seoul = option.chungbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 11){
					ArrayList<String> seoul = option.chungnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 12){
					ArrayList<String> seoul = option.junbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 13){
					ArrayList<String> seoul = option.junnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 14){
					ArrayList<String> seoul = option.kyongbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 15){
					ArrayList<String> seoul = option.kyongnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 16){
					ArrayList<String> seoul = option.jeju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		finish1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(arg2 == 1){
					ArrayList<String> seoul = option.seoul;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 2){
					ArrayList<String> seoul = option.busan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 3){
					ArrayList<String> seoul = option.daegu;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 4){
					ArrayList<String> seoul = option.incheon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 5){
					ArrayList<String> seoul = option.gwangju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 6){
					ArrayList<String> seoul = option.daejun;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 7){
					ArrayList<String> seoul = option.ulsan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 8){
					ArrayList<String> seoul = option.kyonggi;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 9){
					ArrayList<String> seoul = option.kangwon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 10){
					ArrayList<String> seoul = option.chungbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 11){
					ArrayList<String> seoul = option.chungnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 12){
					ArrayList<String> seoul = option.junbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 13){
					ArrayList<String> seoul = option.junnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 14){
					ArrayList<String> seoul = option.kyongbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 15){
					ArrayList<String> seoul = option.kyongnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 16){
					ArrayList<String> seoul = option.jeju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab1Write.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, option.si);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);  
		time1.setAdapter(adapter);

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, option.bun);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);  
		time2.setAdapter(adapter);

		write.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {				
				String start = start1.getSelectedItem().toString() + " "
				+ start2.getSelectedItem().toString();
				String finish = finish1.getSelectedItem().toString() + " "
				+ finish2.getSelectedItem().toString();
				String time = time1.getSelectedItem().toString() + "시 "
				+time2.getSelectedItem().toString() + "분";
				String title = text_title.getText().toString();
				String content = text.getText().toString();

				if(title.equals("")){
					Toast.makeText(BBSCarPoolTab1Write.this, "제목을 입력해주세요.", Toast.LENGTH_SHORT).show();
				}
				else if(title.length() > 20){
					Toast.makeText(BBSCarPoolTab1Write.this, "제목을 20자 이내로 작성해주세요", Toast.LENGTH_SHORT).show();
				}
				else if(content.equals("")){
					Toast.makeText(BBSCarPoolTab1Write.this, "내용을 입력해주세요", Toast.LENGTH_SHORT).show();
				}
				else if(content.length() > 200){
					Toast.makeText(BBSCarPoolTab1Write.this, "내용은 200자 까지 가능합니다", Toast.LENGTH_SHORT).show();
				}
				else if(start.indexOf("광역시/도") != -1 || finish.indexOf("광역시/도") != -1){
					Toast.makeText(BBSCarPoolTab1Write.this, "출발지 목적지를 선택해주세요", Toast.LENGTH_SHORT).show();
				}
				else{
					Intent intent = getIntent();
					intent.putExtra("CONTENT", content);
					intent.putExtra("TITLE", title);
					intent.putExtra("START", start);
					intent.putExtra("FINISH", finish);
					intent.putExtra("TIME", time);
					setResult(RESULT_OK, intent);
					finish();
				}
			}
		});

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

}
